package com.sl.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sl.mapper.CompanyMapper;
import com.sl.pojo.Company;
import com.sl.pojo.Document;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class CompanyService {
    @Resource
    private CompanyMapper companyMapper;
    //查：所有   公司
    public List<Company> findAll(){
      return   companyMapper.selectAll();
    }
    //查：单个  id   公司
    public Company findById(Integer id){
        return companyMapper.selectByPrimaryKey(id);
    }
    //查：条件   公司
    public Page<Company> findList(Map<String,String> searchMap,Integer page,Integer pageSize){
        PageHelper.startPage(page,pageSize);
        Example example = createExample(searchMap);
        List<Company> companies = companyMapper.selectByExample(example);
        return (Page<Company>)companies;
    }
    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, String> searchMap){
        Example example=new Example(Company.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            //  1主键id
            if(searchMap.get("id")!=null ){
                criteria.andEqualTo("id",searchMap.get("id"));
            }
            if(searchMap.get("companyName")!=null ){
                System.out.println("公司名字>>>" +searchMap.get("companyName"));
                criteria.andLike("companyName", "%"+searchMap.get("companyName")+"%");
            }
            //3地址
            if(searchMap.get("companyAddress")!=null && !"".equals(searchMap.get("companyAddress"))){
                criteria.andLike("companyAddress","%"+searchMap.get("companyAddress")+"%");
            }
            //4法人
            if(searchMap.get("legalPerson")!=null && !"".equals(searchMap.get("legalPerson"))){
                criteria.andLike("legalPerson","%"+searchMap.get("legalPerson")+"%");
            }
            //5许可证号
            if(searchMap.get("licenceNum")!=null && !"".equals(searchMap.get("licenceNum"))){
                criteria.andEqualTo("licenceNum",searchMap.get("licenceNum"));
            }
            // 6是否删除
            if(searchMap.get("deleted")!=null ){
                criteria.andEqualTo("deleted",searchMap.get("deleted"));
            }
            //7.主营业务
            if(searchMap.get("mainBusiness")!=null ){
                criteria.andEqualTo("mainBusiness",searchMap.get("mainBusiness"));
            }
            //8.经营项目
            if(searchMap.get("itemsBusiness")!=null ){
                criteria.andEqualTo("itemsBusiness",searchMap.get("itemsBusiness"));
            }
            //9.电话
            if(searchMap.get("telephone")!=null && !"".equals(searchMap.get("telephone"))){
                criteria.andEqualTo("telephone",searchMap.get("telephone"));
            }
            //10.邮箱
            if(searchMap.get("email")!=null ){
                criteria.andEqualTo("email",searchMap.get("email"));
            }
            //11.企业社会信用统一代码
            if(searchMap.get("companyCreditCode")!=null ){
                criteria.andEqualTo("companyCreditCode",searchMap.get("companyCreditCode"));
            }
            //12.企业等级
            if(searchMap.get("companyLevel")!=null ){
                criteria.andEqualTo("companyLevel",searchMap.get("companyLevel"));
            }
            //13.remark备注
            //14企业面积
            if(searchMap.get("companyArea")!=null ){
                criteria.andEqualTo("companyArea",searchMap.get("companyArea"));
            }
        }
        return example;
    }
    //增：一个
    public void addCompany(Company company){companyMapper.insertSelective(company);}
    //更新：公司信息
    public void updateCompany(Company company){
        companyMapper.updateByPrimaryKey(company);
    }

    public void deletedReal(Integer id){
        companyMapper.deleteByPrimaryKey(id);
    }

    public Company findByName(String companyName){
        Example example = new Example(Company.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("companyName", companyName );
        Company company = companyMapper.selectOneByExample(example);
        return company;
    }

}
